/**
 * Copyright 2011 Sibaozuan(superskysbz@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package summary.java.stream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;

/**
 * 
 * @author Sibaozuan(superskysbz@gmail.com)
 * 
 */
public class IOStreamSummary {
	private File testInputFile;
	private File testOutputFile;
	private byte[] byteBuffer = new byte[1024];
	private char[] charBuffer = new char[128];
	private int length = 0;

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public IOStreamSummary(String inPath, String outPath) {
		testFile(inPath, outPath);
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testFile(String inPath, String outPath) {
		testInputFile = new File(inPath);
		testOutputFile = new File(outPath);
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testFileByteStream(String inPath, String outPath) {
		testFile(inPath, outPath);
		try {
			FileInputStream mFileInputStream = new FileInputStream(testInputFile);
			int firstByte = mFileInputStream.read();
			System.out.println("firstByte= " + firstByte);

			FileOutputStream mFileOutputStream = new FileOutputStream(testOutputFile);

			while ((length = mFileInputStream.read(byteBuffer)) != -1) {
				mFileOutputStream.write(byteBuffer, 0, length);
			}
			mFileInputStream.close();
			mFileOutputStream.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testBasicDataStream(String inPath, String outPath) {
		testFile(inPath, outPath);
		try {

			FileOutputStream mFileOutputStream = new FileOutputStream(testOutputFile);
			DataOutputStream mDataOutputStream = new DataOutputStream(mFileOutputStream);

			mDataOutputStream.writeBoolean(true);
			mDataOutputStream.writeByte(10);
			mDataOutputStream.writeChar('a');
			mDataOutputStream.writeDouble(1234.99);
			mDataOutputStream.writeFloat(0.46f);
			mDataOutputStream.writeInt(1000);
			mDataOutputStream.writeLong(1111111111L);
			mDataOutputStream.writeShort(888);
			mDataOutputStream.writeUTF("utf-8");
			mDataOutputStream.close();

			FileInputStream mFileInputStream = new FileInputStream(testInputFile);
			DataInputStream mDataInputStream = new DataInputStream(mFileInputStream);

			mDataInputStream.readBoolean();
			mDataInputStream.readByte();
			mDataInputStream.readChar();
			mDataInputStream.readDouble();
			mDataInputStream.readFloat();
			mDataInputStream.readInt();
			mDataInputStream.readLong();
			mDataInputStream.readShort();
			mDataInputStream.readUTF();
			mDataInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testCharBridgeStream(String inPath, String outPath) {
		testFile(inPath, outPath);
		length = charBuffer.length;
		try {
			FileOutputStream mFileOutputStream = new FileOutputStream(testOutputFile);
			OutputStreamWriter mOutputStreamWriter = new OutputStreamWriter(mFileOutputStream);
			mOutputStreamWriter.write('a');
			mOutputStreamWriter.write("string");
			mOutputStreamWriter.write(charBuffer, 0, length);
			mOutputStreamWriter.write("str", 0, length);
			mOutputStreamWriter.close();
			//
			FileInputStream mFileInputStream = new FileInputStream(testInputFile);
			InputStreamReader mInputStreamReader = new InputStreamReader(mFileInputStream);
			mInputStreamReader.read();
			mInputStreamReader.read(charBuffer);
			mInputStreamReader.read(charBuffer, 0, length);
			mInputStreamReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testBufferCharStream(String inPath, String outPath) {
		testFile(inPath, outPath);
		length = charBuffer.length;
		try {
			FileOutputStream mFileOutputStream = new FileOutputStream(testOutputFile);
			OutputStreamWriter mOutputStreamWriter = new OutputStreamWriter(mFileOutputStream);
			BufferedWriter mBufferedWriter = new BufferedWriter(mOutputStreamWriter);
			mBufferedWriter.write("acdefg");
			mBufferedWriter.newLine();
			mBufferedWriter.write(charBuffer, 0, length);
			mBufferedWriter.close();
			mOutputStreamWriter.close();

			FileInputStream mFileInputStream = new FileInputStream(testInputFile);
			InputStreamReader mInputStreamReader = new InputStreamReader(mFileInputStream);
			BufferedReader mBufferedReader = new BufferedReader(mInputStreamReader);
			String str;
			while ((str = mBufferedReader.readLine()) != null) {
				System.out.println("string= " + str);
			}
			mBufferedReader.close();
			mInputStreamReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testFileCharStream(String inPath, String outPath) {
		testFile(inPath, outPath);
		length = charBuffer.length;
		try {
			FileWriter mFileWriter = new FileWriter(testOutputFile);
			BufferedWriter mBufferedWriter = new BufferedWriter(mFileWriter);
			mBufferedWriter.write("acdefg");
			mBufferedWriter.newLine();
			mBufferedWriter.write(charBuffer, 0, length);
			mBufferedWriter.close();
			mFileWriter.close();

			FileReader mFileReader = new FileReader(testInputFile);
			BufferedReader mBufferedReader = new BufferedReader(mFileReader);
			String str;
			while ((str = mBufferedReader.readLine()) != null) {
				System.out.println("string= " + str);
			}
			mBufferedReader.close();
			mFileReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testInputByKeyboard(String inPath, String outPath) {
		testFile(inPath, outPath);
		try {
			InputStreamReader mInputStreamReader = new InputStreamReader(System.in);
			BufferedReader mBufferedReader = new BufferedReader(mInputStreamReader);
			System.out.println("Enter: by entered no finish");
			//
			FileWriter mFileWriter = new FileWriter(testOutputFile);
			BufferedWriter mBufferedWriter = new BufferedWriter(mFileWriter);
			String str;
			while (true) {
				str = mBufferedReader.readLine();
				if (str.equalsIgnoreCase("no")) {
					break;
				}
				mBufferedWriter.write(str);
				mBufferedWriter.newLine();
			}
			mBufferedWriter.close();
			mFileWriter.close();
			mBufferedReader.close();
			mInputStreamReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param inPath
	 * @param outPath
	 */
	public void testObjectStream(String inPath, String outPath) {
		testFile(inPath, outPath);
		try {
			FileOutputStream mFileOutputStream = new FileOutputStream(testOutputFile);
			ObjectOutputStream mObjectOutputStream = new ObjectOutputStream(mFileOutputStream);
			ObjectStreamTest ost = new ObjectStreamTest("Tom", 30);
			mObjectOutputStream.writeObject(ost);
			mObjectOutputStream.close();
			mFileOutputStream.close();

			FileInputStream mFileInputStream = new FileInputStream(testInputFile);
			ObjectInputStream mObjectInputStream = new ObjectInputStream(mFileInputStream);
			ObjectStreamTest test = (ObjectStreamTest) mObjectInputStream.readObject();
			mObjectInputStream.close();
			mFileInputStream.close();
			test.test();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
